package com.cloudeasy.user.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloudeasy.api.util.EntityUtil;
import com.cloudeasy.api.vo.PageResult;
import com.cloudeasy.api.vo.user.UserVO;
import com.cloudeasy.user.field.UserField;
import com.cloudeasy.user.po.User;
import com.cloudeasy.user.po.UserInfo;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户工具类
 */
public class UserUtil {
    /**
     * 构建
     *
     * @param ids      用户Id列表
     * @param accounts 账号列表
     * @param unlocked 是否开放
     * @param asc      正序排序字段
     * @param desc     倒序排序字段
     * @return {@link QueryWrapper<User>}
     */
    public static QueryWrapper<User> compose(List<String> ids, List<String> accounts, Boolean unlocked, String[] asc, String[] desc) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (ids != null && ids.size() > 0) {
            queryWrapper.in("id", ids);
        }
        if (accounts != null && accounts.size() > 0) {
            queryWrapper.in("account", accounts);
        }
        if (unlocked != null) {
            queryWrapper.eq("unlocked", unlocked);
        }
        if (asc != null && asc.length > 0) {
            EntityUtil.checkParamter(UserField.getFieldList(), asc);
            queryWrapper.orderByAsc(asc);
        }
        if (desc != null && desc.length > 0) {
            EntityUtil.checkParamter(UserField.getFieldList(), desc);
            queryWrapper.orderByDesc(desc);
        }
        return queryWrapper;
    }

    /**
     * 转换VO对象
     *
     * @param user     {@link User}
     * @param userInfo {@link UserInfo}
     * @return {@link UserVO}
     */
    public static UserVO convert(User user, UserInfo userInfo) {
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        BeanUtils.copyProperties(userInfo, userVO);
        return userVO;
    }

    /**
     * 转换VO对象
     *
     * @param users     {@link List<User>}
     * @param userInfos {@link List<UserInfo>}
     * @return {@link List<UserVO>}
     */
    public static List<UserVO> convert(List<User> users, List<UserInfo> userInfos) {
        Map<String, UserInfo> userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo, (o, o2) -> o));
        return users.stream().map(user -> convert(user, userInfoMap.get(user.getId()))).collect(Collectors.toList());
    }

    /**
     * 转换VO对象
     *
     * @param userIPage {@link IPage<User>}
     * @param userInfos {@link List<UserInfo>}
     * @return {@link PageResult<UserVO>}
     */
    public static PageResult<UserVO> convert(IPage<User> userIPage, List<UserInfo> userInfos) {
        Map<String, UserInfo> userInfoMap = userInfos.stream().collect(Collectors.toMap(UserInfo::getId, userInfo -> userInfo, (o, o2) -> o));
        PageResult<UserVO> userVOPageResult = new PageResult<>();
        userVOPageResult.setPages(userIPage.getPages())
                .setTotal(userIPage.getTotal())
                .setRecords(userIPage.convert(user -> convert(user, userInfoMap.get(user.getId()))).getRecords());
        return userVOPageResult;
    }
}
